package com.bbva.gptf.back.dominio;

import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Configurable
public class SpProNocturno {
	
	private SimpleJdbcCall simpleJdbcCall;
    private DataSource dataSource;

    @Autowired
    public void setDataSource(DataSource dataSource) {
          this.dataSource = dataSource;
    }

    @PersistenceContext
    transient EntityManager entityManager;
	
	@Transactional
    public Boolean execute(Date fechaCierre){
    	String procedureName = "PKG_GPT.SP_PRO_NOCTURNO";
        this.simpleJdbcCall = new SimpleJdbcCall(this.dataSource);
        this.simpleJdbcCall.withoutProcedureColumnMetaDataAccess();
        this.simpleJdbcCall.withProcedureName(procedureName);
        this.simpleJdbcCall.declareParameters(new SqlParameter("p_fecha_cierre", Types.DATE));
        this.simpleJdbcCall.declareParameters(new SqlOutParameter("p_error", Types.VARCHAR));
        
        Map<String, Object> parametros = new HashMap<String, Object>();
        parametros.put("p_fecha_cierre", fechaCierre);
        Map<String, Object> out = this.simpleJdbcCall.execute(parametros);
        String error = (String) out.get("p_error");
        return error != null ? false : true;
	}
}
